#include "protocol13761.h"

protocol13761::protocol13761()
{

}
int protocol13761::make376Frame(objRequest  *pRequest)
{
    INT16U i;
    INT16U frameLen;
    INT8U cs;
    pRequest->send_frame[ 0 ] = 0x68;
    pRequest->send_frame[ 5 ] = 0x68;
    //3.1 减去固定长度帧报文头
    pRequest->pos -= 6;

    //3.2 数据长度左移2位
    pRequest->pos<<=2;
    pRequest->pos += 2;
    //3.4 设置长度
       pRequest->send_frame[1] =  (pRequest->pos)&0xFF;
       pRequest->send_frame[2] =  (pRequest->pos >> 8)&0xFF;
       pRequest->send_frame[3] =  pRequest->send_frame[1];
       pRequest->send_frame[4] =  pRequest->send_frame[2];
       //提取用户数据区长度
       frameLen = bin2_int16u(pRequest->send_frame+1);
       frameLen >>=2;
       //计算校验位
          cs=0;
          frameLen += 6;   //加上固定长度的报文头
          for(i=6;i<frameLen;i++)  cs += pRequest->send_frame[i];
          pRequest->send_frame[frameLen++]=cs;
          //设置结束字符
          pRequest->send_frame[frameLen++]=0x16;

          pRequest->frame_len = frameLen;
          return 0;
}
void protocol13761::protocol_service_check_header( channel_param   *serial_channel)
{
    if(check_has_received_available_header(serial_channel))
      {
        memcpy(serial_channel->recevice_frame_waite,serial_channel->receive_head,RECE_FRAME_HEAD);
            serial_channel->receive_state = APP_FRAME_RECV_FRAME;
            serial_channel->receive_len = RECE_FRAME_HEAD;
      }
}
BOOLEAN protocol13761::check_has_received_available_header( channel_param   *serial_channel1)
{
        if(is_376_simple_protocol_frame_header( (INT8U*)serial_channel1->receive_head,RECE_FRAME_HEAD))
        {
            qDebug()<<"frame head check through";
            return TRUE;
        }
        else
        {
            return FALSE;
        }
}
INT8U  protocol13761::is_376_simple_protocol_frame_header(INT8U *frame, INT16U recv_len)
{
    //检查376.1报文头
    if((0x68 == frame[0])  && (0x68 == frame[5]) )
    {
         //长度域检查
         if( (frame[1]==frame[3]) && (frame[2]==frame[4]) )
         {
              return TRUE;
         }
    }
    return FALSE;
}
INT16U  protocol13761::get_376_simple_protocol_frame_len(INT8U *frame, INT16U recv_len)
{
    INT16U  frameLen;

          frameLen = frame[2];
          frameLen <<= 8;
          frameLen += frame[1];
          frameLen >>=2;

          frameLen += 8;

          return frameLen;
}
int  protocol13761::protocol_service_check_frame(channel_param   *serial_channel)
{
        INT16U frameLen = get_376_simple_protocol_frame_len((INT8U*)serial_channel->recevice_frame_waite,serial_channel->receive_len);

         if(is_simple_376_protocol_frame_ready((INT8U*)serial_channel->recevice_frame_waite,frameLen,serial_channel->receive_len))
         {
             qDebug()<<"full frame is ok";
             serial_channel->receive_state = APP_FRAME_RECV_HEADER;
             //如果有延时回调，那就执行，否则就进行确认与否认的判断。
            if(serial_channel->need_show_flag)
            {
                //serial_channel->show((INT8U*)serial_channel->recevice_frame_waite,serial_channel->receive_len);
              return 1;
            }
            else
            {
                return 2;
            }
         }
         return FALSE;
}
/*+++
  功能：  初始化376.1报文接收的校验值，已经接收到了完整的报文头
  备注：
          1）需要考虑报文已经全部接收完成了的情况。
---*/
INT16U protocol13761::init_simple_376protocol_frame_checksum(INT8U *frame, INT16U frame_len, INT16U recv_len)
{
   INT16U idx,count;
   INT16U  cs;

   //确定计算长度
   if(recv_len <= frame_len - 2)
   {
       count  = recv_len;
   }
   else
   {
      count = frame_len - 2;
   }

   cs = 0;
   for(idx=6; idx < count ; idx++)
   {
      cs += frame[idx];
   }

   return cs;

}
//判断376报文是不是接收完成了。
INT8U protocol13761::is_simple_376_protocol_frame_ready(INT8U *frame, INT16U frame_len, INT16U recv_len)
{
    INT16U cs;
    if(recv_len >= frame_len)
    {
        cs = init_simple_376protocol_frame_checksum(frame,frame_len,recv_len);
        if(  (((INT8U)(cs)) == frame[frame_len-2])  &&  (0x16 == frame[frame_len-1])  )
        {
           return TRUE;
        }
        return FALSE;
    }
    else if(recv_len <= frame_len - 2)
    {
        cs += frame[recv_len - 1];
    }

    return FALSE;
}
